package com.yj.springbootjava8.map;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class MapMergeTest {
    static List<StudentScore> studentScoreList;
    static {

        studentScoreList = new ArrayList<>();
        StudentScore studentScore1 = new StudentScore("张三","语文",70.0);
        StudentScore studentScore2 = new StudentScore("张三","数学",80.0);
        StudentScore studentScore3 = new StudentScore("张三","英语",80.0);

        StudentScore studentScore4 = new StudentScore("李四","语文",60.0);
        StudentScore studentScore5 = new StudentScore("李四","数学",70.0);
        StudentScore studentScore6 = new StudentScore("李四","英语",85.0);

        StudentScore studentScore7 = new StudentScore("王五","语文",65.0);
        StudentScore studentScore8 = new StudentScore("王五","数学",75.0);
        StudentScore studentScore9 = new StudentScore("王五","英语",95.0);

        studentScoreList.add(studentScore1);
        studentScoreList.add(studentScore2);
        studentScoreList.add(studentScore3);
        studentScoreList.add(studentScore4);
        studentScoreList.add(studentScore5);
        studentScoreList.add(studentScore6);
        studentScoreList.add(studentScore7);
        studentScoreList.add(studentScore8);
        studentScoreList.add(studentScore9);
    }


    public static Map merge1(){

       Map<String,Object> map =  new HashMap<>();

        for (StudentScore studentScore : studentScoreList) {
            if (map.containsKey(studentScore.getStuName())){
                Double score = (Double) map.get(studentScore.getStuName());
                map.put(studentScore.getStuName(),score + studentScore.getScore());
            }else {
                map.put(studentScore.getStuName(),studentScore.getScore());
            }
        }

        return map;

    }


    public static Map merge2(){
        Map<String,Double> map =  new HashMap<>();
        //studentScoreList.forEach(studentScore -> {map.merge(studentScore.getStuName(),studentScore.getScore(),Double::sum);});


        studentScoreList.forEach(studentScore -> {

            map.compute(studentScore.getStuName(),(s, aDouble) ->{
                System.out.println(s);
                System.out.println(aDouble);
                return aDouble;
            });

        });

        return map;

    }

    public static void main(String[] args) {
        Map map = merge2();
        System.out.println(map);
    }





}
